package banner

import (
	"fmt"
	"myblogs/app/variable"
	"time"
)

type BannerModel struct {
	Id           int64
	Title        string
	Summary      string
	ImageUrl     string `db:"image_url"`
	Href         string
	Sort         int64
	Status       int8
	IsDel        int8      `db:"is_del"`
	CreatedAt    time.Time `db:"created_at"`
	CreatedAtStr string
}

// GetBannerList 文章列表
func (a *BannerModel) GetBannerList() []*BannerModel {
	sqlStr := "SELECT id, title, summary, image_url, href, sort, status, is_del, created_at " +
		"FROM banner " +
		"WHERE status=? ORDER BY sort ASC"
	var banners []*BannerModel
	err := variable.Db.Select(&banners, sqlStr, 1)
	if err != nil {
		fmt.Printf("%v \n", err)
		return nil
	}

	for _, banner := range banners {
		banner.ImageUrl = variable.ImgHost + banner.ImageUrl
		banner.CreatedAtStr = banner.CreatedAt.Format("2006-01-02")
	}

	return banners
}
